package com.java.IO.buffered_stream;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterAPI {
    public static void main(String[] args) throws IOException {
        String path = "File和IO流\\com\\java\\files\\char_stream.txt";
        
        // 创建对象：创建规则主要依赖于构造形参Writer的子类
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
        
        // write：写出数据到缓冲区，若参数为整数则会转为ASCII对应的字符。注意此时写出目标地内是无写出数据的
        bufferedWriter.write(97); // a
        bufferedWriter.write('9'); // 9
        bufferedWriter.write('中'); // 中
        bufferedWriter.write('中'); // 中
        bufferedWriter.write("\r\n"); // 换行(Windows\r\n、Linux\n、Mac\r)
        bufferedWriter.write(new char[]{'a', '哈', 'c', 'd'}); // a哈cd
        bufferedWriter.write(new char[]{'a', '哈', 'c', 'd', 101, 102, 103}, 3, 2); // de
        bufferedWriter.write("啊啊啊Hello"); // 啊啊啊Hello
        bufferedWriter.write("啊啊啊Hello", 3, 2); // He
        
        // newLine：写出换行符到缓冲区，换行符可兼容各个系统
        bufferedWriter.newLine();
        
        // flush：缓冲区数据写出到目标地并清空缓冲区。打断点到此并观察断点执行前后目标地数据
        bufferedWriter.flush();
        
        // close：关闭流并释放资源，若此前未执行flush则充当部分flush作用将缓冲区数据写出到目标地
        bufferedWriter.close();
    }
}
